﻿using Kinlo.Entitys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UIWindows.Domains;
using UIWindows.Enums;
using UIWindows.Views;
using XingLucifer.IBase.Enums;
using static log4net.Appender.RollingFileAppender;

namespace UIWindows.Tools.Helper
{
    public static class SqlHelper
    {

        /// <summary>
        /// 查询获取电池数据
        /// </summary>
        /// <param name="dateTime">查询时间</param>
        /// <param name="message">前称重 后称重等</param>
        /// <param name="snowflakeId">雪花ID</param>
        /// <returns></returns>
        public static ProductionData GetEntity(this DateTime dateTime, TaskTypes message, long snowflakeId, string suffix)
        {
            var tableName = $"ProductionData{suffix}_{dateTime:yyyy}{dateTime.GetQuarterly()}";//查当前季度数据
            ProductionData.Meta.TableName = tableName;
            //ProductionData.Meta.TableName = $"ProductionData_{dateTime:yyyy}{dateTime.GetQuarterly()}";
            ProductionData entity = null;
            try
            {
                entity = ProductionData.Find(ProductionData._.SnowflakeId == snowflakeId);
            }
            catch (Exception ex)
            {
                $"{message} 雪花ID:{snowflakeId} 数据库操作错误：{ex}".RunLog(MessageLevelType.警告);
            }
            if (entity == null)
            {
                if (dateTime.ContrastTime())
                {
                    dateTime = dateTime.AddDays(-10);
                    return dateTime.GetEntity(message, snowflakeId, suffix);
                }
                $"{message} 找不到雪花ID为：[{snowflakeId}]".RunLog(MessageLevelType.警告);
            }
            return entity;
        }

        /// <summary>
        /// 获取第一次注液信息
        /// </summary>
        /// <param name="dateTime">查询时间</param>
        /// <param name="message">前称重 后称重等</param>
        /// <param name="barcode">电池条码</param>
        /// <param name="isrun">是否执行过</param>
        /// <returns></returns>
        public static ProductionData GetHistoryEntity(this DateTime dateTime, string message, string barcode, string suffix)
        {
            var tableName = $"ProductionData{suffix}_{dateTime:yyyy}{dateTime.GetQuarterly()}";//查当前季度数据
            ProductionData.Meta.TableName = tableName;
            //ProductionData.Meta.TableName = $"ProductionData_{dateTime:yyyy}{dateTime.GetQuarterly()}";
            int oldDay = dateTime.GetQuarterly(), newDay = (dateTime.AddDays(-5)).GetQuarterly();
            XCode.Expression expression = null;
            if (newDay == oldDay)
            {
                long startTime = SnowflakeManager.GetTimeId(new DateTime(dateTime.Year, dateTime.Month, dateTime.Day - 5, 0, 0, 0, 0, DateTimeKind.Local));
                long endTime = SnowflakeManager.GetTimeId(new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, 23, 59, 59, 999, DateTimeKind.Local));
                expression = ProductionData._.SnowflakeId >= startTime & ProductionData._.SnowflakeId <= endTime & ProductionData._.BarCode == barcode & ProductionData._.ResultFormerWeighing == 1;
            }
            else
            {
                expression = ProductionData._.BarCode == barcode & ProductionData._.ResultFormerWeighing == 1;
            }
            var entity = ProductionData.FindAll(expression);
            IList<ProductionData> data;
            if (dateTime.ContrastTime())
            {
                dateTime = dateTime.AddDays(-10);
                ProductionData.Meta.TableName = $"ProductionData_{dateTime:yyyy}{dateTime.GetQuarterly()}";
                newDay = (dateTime.AddDays(-5)).GetQuarterly();
                oldDay = dateTime.GetQuarterly();
                if (newDay == oldDay)
                {
                    long startTime = SnowflakeManager.GetTimeId(new DateTime(dateTime.Year, dateTime.Month, dateTime.Day - 5, 0, 0, 0, 0, DateTimeKind.Local));
                    long endTime = SnowflakeManager.GetTimeId(new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, 23, 59, 59, 999, DateTimeKind.Local));
                    expression = ProductionData._.SnowflakeId >= startTime & ProductionData._.SnowflakeId <= endTime & ProductionData._.BarCode == barcode & ProductionData._.ResultFormerWeighing == 1;
                }
                else
                {
                    expression = ProductionData._.BarCode == barcode & ProductionData._.ResultFormerWeighing == 1;
                }
                data = ProductionData.FindAll(expression);
                if (data.Count > 0)
                {
                    if (data.Count > 1) return data.FirstOrDefault(x => x.SnowflakeId == data.Min(x => x.SnowflakeId));
                    else return data[0];
                }
                var model = ProductionData.Find(ProductionData._.BarCode == barcode & ProductionData._.ResultFormerWeighing == 1);
                if (model != null) return model;
            }
            if (entity.Count == 0)
            {
                $"{message} 找不到【{barcode}】条码".RunLog(MessageLevelType.错误);
                return null;
            }
            else
            {
                if (entity.Count > 1) return entity.FirstOrDefault(x => x.SnowflakeId == entity.Min(x => x.SnowflakeId));
                else return entity[0];
            }
        }


        ///// <summary>
        ///// 获取最新的数据进行补液操作
        ///// </summary>
        ///// <param name="dateTime">查询时间</param>
        ///// <param name="message">前称重 后称重等</param>
        ///// <param name="barcode">电池条码</param>
        ///// <returns></returns>
        //public static ProductionData GetNewEntity(this DateTime dateTime, string message, string barcode, string suffix)
        //{
        //    var tableName = $"ProductionData{suffix}_{dateTime:yyyy}{dateTime.GetQuarterly()}";//查当前季度数据
        //    ProductionData.Meta.TableName = tableName;
        //    //ProductionData.Meta.TableName = $"ProductionData_{dateTime:yyyy}{dateTime.GetQuarterly()}";
        //    DateTime start = dateTime.AddDays(-7);
        //    int oldDay = dateTime.GetQuarterly(), newDay = start.GetQuarterly();
        //    XCode.Expression expression = null;
        //    if (newDay == oldDay)
        //    {
        //        long startTime = SnowflakeManager.GetTimeId(new DateTime(start.Year, start.Month, start.Day, 0, 0, 0, 0, DateTimeKind.Local));
        //        long endTime = SnowflakeManager.GetTimeId(new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, 23, 59, 59, 999, DateTimeKind.Local));
        //        expression = ProductionData._.SnowflakeId >= startTime & ProductionData._.SnowflakeId <= endTime & ProductionData._.BarCode == barcode & ProductionData._.ResultFormerWeighing == 1;
        //    }
        //    else
        //    {
        //        expression = ProductionData._.BarCode == barcode & ProductionData._.ResultFormerWeighing == 1;
        //    }
        //    var entity = ProductionData.FindAll(expression);
        //    if (entity.Count != 0)
        //    {
        //        if (entity.Count > 1) return entity.FirstOrDefault(x => x.SnowflakeId == entity.Max(x => x.SnowflakeId));
        //        else return entity[0];
        //    }
        //    if (dateTime.ContrastTime())
        //    {
        //        IList<ProductionData> data;
        //        dateTime = dateTime.AddDays(-1);
        //        ProductionData.Meta.TableName = $"ProductionData_{dateTime:yyyy}{dateTime.GetQuarterly()}";
        //        start = dateTime.AddDays(-7);
        //        newDay = start.GetQuarterly();
        //        oldDay = dateTime.GetQuarterly();
        //        if (newDay == oldDay)
        //        {
        //            long startTime = SnowflakeManager.GetTimeId(new DateTime(start.Year, start.Month, start.Day, 0, 0, 0, 0, DateTimeKind.Local));
        //            long endTime = SnowflakeManager.GetTimeId(new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, 23, 59, 59, 999, DateTimeKind.Local));
        //            expression = ProductionData._.SnowflakeId >= startTime & ProductionData._.SnowflakeId <= endTime & ProductionData._.BarCode == barcode & ProductionData._.ResultFormerWeighing == 1;
        //        }
        //        else
        //        {
        //            expression = ProductionData._.BarCode == barcode & ProductionData._.ResultFormerWeighing == 1;
        //        }
        //        data = ProductionData.FindAll(expression);
        //        if (data.Count > 0)
        //        {
        //            if (data.Count > 1) return data.FirstOrDefault(x => x.SnowflakeId == data.Max(x => x.SnowflakeId));
        //            else return data[0];
        //        }
        //        var model = ProductionData.Find(ProductionData._.BarCode == barcode & ProductionData._.ResultFormerWeighing == 1);
        //        if (model != null) return model;
        //    }
        //    $"{message} 找不到【{barcode}】条码".RunLog(MessageLevelType.错误);
        //    return null;
        //}

        public static ProductionData GetNewEntity(this DateTime dateTime, string message, string barcode, string suffix="One")
        {
            var tableName = $"ProductionData{suffix}_{dateTime:yyyy}{dateTime.GetQuarterly()}";//查当前季度数据
            ProductionData.Meta.TableName = tableName;
            XCode.Expression expression = null;
            expression = ProductionData._.BarCode == barcode;
            var entitys = ProductionData.FindAll(expression);
            if (entitys.Count > 0)
            {
                return entitys.First(x => x.SnowflakeId == entitys.Max(x => x.SnowflakeId));
            }

            dateTime=dateTime.AddMonths(-3);
            tableName = $"ProductionData{suffix}_{dateTime:yyyy}{dateTime.GetQuarterly()}";//查当前季度数据查不到 再查上一季度
            ProductionData.Meta.TableName = tableName;
            entitys = ProductionData.FindAll(expression);
            if (entitys.Count > 0)
            {
                return entitys.First(x => x.SnowflakeId == entitys.Max(x => x.SnowflakeId));
            }
            else
            {
                $"{message} 找不到【{barcode}】条码".RunLog(MessageLevelType.错误);
                return null;
            }
        }
    }
}
